package com.htfyun.unisound.serialmotor.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;


import com.htfyun.hardware.leds.BreatheLedAnimationAttr;
import com.htfyun.hardware.leds.BreatheLedBatchOperationInfo;
import com.htfyun.hardware.leds.BreatheLedController;
import com.htfyun.hardware.leds.BreatheLedType;
import com.htfyun.unisound.serialmotor.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by sst on 2018/7/11.
 */

public class TestBreatheLedFragment extends Fragment
        implements View.OnClickListener,
        DialogInterface.OnMultiChoiceClickListener,
        DialogInterface.OnClickListener {

    private final static String TAG = "BreatheLedTest";

    private EditText editLight;
    private EditText editBrightness;

    private TextView txtBreatheLedInfo;
    private TextView txtLightSetInfo;

    private LedSelectedDialogFragment ledSelectedDialog;
    private LedSelectAttr ledSelectAttr = LedSelectAttr.unknown;

    private Switch switchLedSuspendLeft;
    private Switch switchLedSuspendRight;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext())
                .inflate(getLayoutId(), container, false);
        initView(view, savedInstanceState);

        return view;
    }

    protected int getLayoutId() {
        return R.layout.fragment_led;
    }

    protected void initView(View view, Bundle savedInstanceState) {

        view.findViewById(R.id.btnAllOn).setOnClickListener(this);
        view.findViewById(R.id.btnAllOff).setOnClickListener(this);
        view.findViewById(R.id.btnBreatheOn).setOnClickListener(this);
        view.findViewById(R.id.btnMarqueeOn).setOnClickListener(this);
        view.findViewById(R.id.btnLightSelectLeft).setOnClickListener(this);
        view.findViewById(R.id.btnLightSelectRight).setOnClickListener(this);
        view.findViewById(R.id.btnLeftLedCtrlInfo).setOnClickListener(this);
        view.findViewById(R.id.btnRightLedCtrlInfo).setOnClickListener(this);
        view.findViewById(R.id.btnSetBrightness).setOnClickListener(this);

        txtBreatheLedInfo = (TextView) view.findViewById(R.id.txtBreatheLedInfo);
        txtLightSetInfo = (TextView) view.findViewById(R.id.txtLightSetInfo);


        BreatheLedController ledController = BreatheLedController.INSTANCE;

        int leftNum = ledController.getLedNumbers(BreatheLedType.left);
        int rightNum = ledController.getLedNumbers(BreatheLedType.right);
        int maxLight = ledController.getMaxLedBrightness(BreatheLedType.left);
        int minLight = ledController.getMinLedBrightness(BreatheLedType.left);

        txtBreatheLedInfo.setText(getString(R.string.breathe_led_info, leftNum, rightNum, maxLight, minLight));

        txtLightSetInfo.setText(getString(R.string.breathe_led_light_set_info, maxLight, minLight));

        editBrightness = (EditText) view.findViewById(R.id.editBrightness);
        editBrightness.setText(String.format(Locale.getDefault(),"%d", maxLight));

        editLight = (EditText) view.findViewById(R.id.editLight);

        editLight.setText(String.format(Locale.getDefault(),"%d", maxLight));


        switchLedSuspendLeft = (Switch) view.findViewById(R.id.switchLedSuspendLeft);
        switchLedSuspendRight = (Switch) view.findViewById(R.id.switchLedSuspendRight);
        switchLedSuspendLeft.setOnCheckedChangeListener(onCheckedChangeListener);
        switchLedSuspendRight.setOnCheckedChangeListener(onCheckedChangeListener);

        initSwitchLedSuspend();
        Log.e(TAG, "onCreateView");
    }

    private void initSwitchLedSuspend() {
        if (switchLedSuspendLeft != null) {
            switchLedSuspendLeft.setChecked(BreatheLedController.INSTANCE.isLedOffInSuspend(BreatheLedType.left));
        }

        if (switchLedSuspendRight != null) {
            switchLedSuspendRight.setChecked(BreatheLedController.INSTANCE.isLedOffInSuspend(BreatheLedType.right));
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        Log.e(TAG, "setUserVisibleHint isVisibleToUser = " + isVisibleToUser);

        isResumed = isVisibleToUser;

        if (isVisibleToUser) {

            initSwitchLedSuspend();
        }
    }

    boolean isResumed = false;

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (!isResumed) {
                        return;
                    }

                    if (buttonView == switchLedSuspendLeft) {

                        BreatheLedController.INSTANCE.setLedOffInSuspend(isChecked, BreatheLedType.left);

                    } else if (buttonView == switchLedSuspendRight) {

                        BreatheLedController.INSTANCE.setLedOffInSuspend(isChecked, BreatheLedType.right);

                    }

                }
            };


    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.btnAllOn:
                BreatheLedController.INSTANCE.setAllLedLight(true, BreatheLedType.left, BreatheLedType.right);
                break;
            case R.id.btnAllOff:
                BreatheLedController.INSTANCE.setAllLedLight(false, BreatheLedType.left, BreatheLedType.right);

                BreatheLedController.INSTANCE.stopAnimation(BreatheLedType.left, BreatheLedType.right);
                break;
            case R.id.btnBreatheOn:{
                BreatheLedController.INSTANCE.startAnimation(BreatheLedAnimationAttr.breathe, (long)100, 0, BreatheLedType.right, BreatheLedType.left);

                break;}
            case R.id.btnMarqueeOn:{
                BreatheLedController.INSTANCE.startAnimation(BreatheLedAnimationAttr.marquee, (long)100, 0, BreatheLedType.right, BreatheLedType.left);
                break;}
            case R.id.btnLeftLedCtrlInfo: {


                List<BreatheLedBatchOperationInfo> ledBatchOperationInfos = new ArrayList<>();
                int brightness = 120;
                int time = 500;
                for(int i = 0; i < BreatheLedController.INSTANCE.getLedNumbers(BreatheLedType.left); i++) {
                    ledBatchOperationInfos.add(new BreatheLedBatchOperationInfo(i, brightness, time));
                }

                BreatheLedController.INSTANCE.setLedLightInBatchOperation(ledBatchOperationInfos, BreatheLedType.left);


                break;
            }
            case R.id.btnRightLedCtrlInfo: {

                List<BreatheLedBatchOperationInfo> ledBatchOperationInfos = new ArrayList<>();
                int brightness = 120;
                int time = 500;
                for(int i = 0; i < BreatheLedController.INSTANCE.getLedNumbers(BreatheLedType.right); i++) {
                    ledBatchOperationInfos.add(new BreatheLedBatchOperationInfo(i, brightness, time));
                }

                BreatheLedController.INSTANCE.setLedLightInBatchOperation(ledBatchOperationInfos, BreatheLedType.right);


                break;
            }

            case R.id.btnLightSelectLeft:
                showLedSelectedDialog(LedSelectAttr.left);

                break;
            case R.id.btnLightSelectRight:
                showLedSelectedDialog(LedSelectAttr.right);

                break;
            case R.id.btnSetBrightness: {


                break;
            }

                default:break;
        }
    }

    private void showLedSelectedDialog(LedSelectAttr attr) {

        ledSelectAttr = attr;

        if (ledSelectAttr == LedSelectAttr.unknown) {
            return;
        }
        if (ledSelectedDialog == null) {
            ledSelectedDialog = new LedSelectedDialogFragment();
        }

        ledSelectAttr.getList().clear();

        String title = getString(R.string.breathe_led_light_select_left);
        int ledNum = BreatheLedController.INSTANCE.getLedNumbers(BreatheLedType.left);

        if (ledSelectAttr.getType() == BreatheLedType.right) {
            title = getString(R.string.breathe_led_light_select_right);
            ledNum = BreatheLedController.INSTANCE.getLedNumbers(BreatheLedType.right);
        }

        String[] items = new String[ledNum + 3];
        items[0] = getString(R.string.led_selected_all);
        items[1] = getString(R.string.led_selected_odd);
        items[2] = getString(R.string.led_selected_even);

        for (int i = 3; i < items.length; i++) {
            items[i] = String.format(Locale.getDefault(), "%d", i - 2);
        }

        ledSelectedDialog.show(title, items, this, this, getFragmentManager());
    }

    private int getLightInEditLight() {
        String text = editLight.getText().toString();

        try {

            return Integer.decode(text);

        } catch (Exception e) {
        }

        return BreatheLedController.INSTANCE.getMaxLedBrightness(ledSelectAttr.getType());
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {

        //ok clicked
        List<Integer> list = ledSelectAttr.getList();



        for (Integer id : list) {

            BreatheLedController.INSTANCE.setLedLight(ledSelectAttr.getType(), id, getLightInEditLight());

        }

    }

    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {


        if (which == 0) {
            //all

            for (int i = 0; i < BreatheLedController.INSTANCE.getLedNumbers(ledSelectAttr.getType()); i++) {
                BreatheLedController.INSTANCE.setLedLight(ledSelectAttr.getType(),i, getLightInEditLight());
            }


            dialog.dismiss();

        } else if (which == 1) {
            //odd

            for (int i = 0; i < BreatheLedController.INSTANCE.getLedNumbers(ledSelectAttr.getType()); i++) {
                if ((i + 1) % 2 != 0) {
                    BreatheLedController.INSTANCE.setLedLight(ledSelectAttr.getType(), i, getLightInEditLight());
                }
            }


            dialog.dismiss();


        } else if (which == 2) {
            //even

            for (int i = 0; i < BreatheLedController.INSTANCE.getLedNumbers(ledSelectAttr.getType()); i++) {
                if ((i + 1) % 2 == 0) {
                    BreatheLedController.INSTANCE.setLedLight(ledSelectAttr.getType(), i, getLightInEditLight());
                }
            }

            dialog.dismiss();


        } else {
            //single

            List<Integer> list = ledSelectAttr.getList();
            if (list != null) {
                list.add(which - 3);
            }

        }

    }

    private enum LedSelectAttr {

        unknown(BreatheLedType.left),
        left(BreatheLedType.left),
        right(BreatheLedType.right),
        ;
        private final BreatheLedType type;
        private final List<Integer> list;

        LedSelectAttr(BreatheLedType type) {
            this.type = type;
            this.list = new ArrayList<Integer>();
        }

        public BreatheLedType getType() {
            return type;
        }

        public List<Integer> getList() {
            return list;
        }
    }

}
